import axios from './axios' // 用于 FormData提交形式
import axioss from './axioss' // 用于 body payload 提交形式
import { Toast } from 'vant'
import Vue from 'vue'
let vue = new Vue()
// let SERVICE_PORTAL = process.env.VUE_APP_SERVICE_PORTAL
export function get (url, params = {}, config, cb) {
  return new Promise((resolve, reject) => {
    let finalUrl = ''
    // 线下
    // if (url.indexOf(`${SERVICE_PORTAL}`) !== -1) {
    //   finalUrl = url
    // } else {
    //   finalUrl = '/platform' + url
    // }
    // 线上
    finalUrl = '/roche2/api' + url
    axios
      .get(finalUrl, {
        params,
        ...config
      })
      .then(rsp => resolve(rsp))
      .catch(e => {
        if (cb && typeof cb === 'function') {
          cb(e)
          return false
        }
        handleError(e)
        reject(e)
      })
  })
}

export function post (url, params, config, cb) {
  return new Promise((resolve, reject) => {
    let finalUrl = ''
    // 线下
    // if (url.indexOf(`${SERVICE_PORTAL}`) !== -1) {
    //   finalUrl = url
    // } else {
    //   finalUrl = '/platform' + url
    // }
    // 线上
    finalUrl = '/roche2/api' + url
    axios
      .post(finalUrl, params, config)
      .then(rsp => resolve(rsp))
      .catch(e => {
        if (cb && typeof cb === 'function') {
          cb(e)
          return false
        }
        handleError(e)
        reject(e)
      })
  })
}

export function gets (url, params = {}, config, cb) {
  return new Promise((resolve, reject) => {
    let finalUrl = ''
    // 线下
    // if (url.indexOf(`${SERVICE_PORTAL}`) !== -1) {
    //   finalUrl = url
    // } else {
    //   finalUrl = '/platform' + url
    // }
    // 线上
    finalUrl = '/roche2/api' + url
    axioss
      .get(finalUrl, {
        params,
        ...config
      })
      .then(rsp => resolve(rsp))
      .catch(e => {
        if (cb && typeof cb === 'function') {
          cb(e)
          return false
        }
        handleError(e)
        reject(e)
      })
  })
}

export function posts (url, params, config, cb) {
  return new Promise((resolve, reject) => {
    let finalUrl = ''
    // 线下
    // if (url.indexOf(`${SERVICE_PORTAL}`) !== -1) {
    //   finalUrl = url
    // } else {
    //   finalUrl = '/platform' + url
    // }
    // 线上
    finalUrl = '/roche2/api' + url
    axioss
      .post(finalUrl, params, config)
      .then(rsp => resolve(rsp))
      .catch(e => {
        if (cb && typeof cb === 'function') {
          cb(e)
          return false
        }
        handleError(e)
        reject(e)
      })
  })
}

// 全局ajax错误回调
export function handleError (e) {
  let str = e + ''
  // console.log('handleError str: ', str)
  if (e.toString() === 'Cancel') {
    console.log('请求已取消！')
    return
  }
  // console.log('handleError response: ', e.response)
  // console.log('handleError toString: ', e.toString())
  let msg = `请求无响应，请稍后再试`
  if (str.search('timeout') !== -1) {
    msg = '请求超时，请稍后再试'
  } else if (~str.search('Network Error')) {
    msg = '请检查您的网络'
  }
  if (e.response && e.response.data) {
    msg = e.response.data.message
  }
  if (!msg) {
    msg = '连接异常，请稍后再试'
  }
  Toast.clear()
  Toast({
    message: msg,
    onClose: () => {
      vue.$_load.hide()
    }
  })
}
